/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.regex.Pattern;

/**
 * Created by Peter on 15/12/2.
 * update by cherry on 16/09/18
 * Changed by Tony.yu on 16/09/26.  pattern = Pattern.compile("-?[0-9]+(\\.?)[0-9]*");
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * Validate the source is numeric.
     *
     * @param str source string
     * @return true if it's numeric.
     */
    public static boolean isNumeric(String str) {
        if (StringUtils.isSpecialBlank(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("-?[0-9]+(\\.?)[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * Validate the source is blank.
     * "N/A", "-","nil","UnKown Line", "_" will be seem the same as null or empty.
     *
     * @param data source string
     * @return true if it's blank.
     */
    public static boolean isSpecialBlank(String data) {
        if (StringUtils.isBlank(data)) {
            return true;
        }

        if (data.equalsIgnoreCase("N/A") || data.equalsIgnoreCase("-")
                || data.equalsIgnoreCase("nil") || data.equalsIgnoreCase("UnKown Line")
                || data.equalsIgnoreCase("_")) {
            return true;
        }

        return false;
    }

    /**
     * Validate the source is blank.
     * "N/A", "-","nil","NaN", "_" will be seem the same as null or empty.
     *
     * @param d source string
     * @return true if it's blank.
     */
    public static boolean isBlankWithSpecialNumber(String d) {
        if (StringUtils.isBlank(d)) {
            return true;
        }

        if (d.equalsIgnoreCase("N/A") || d.equalsIgnoreCase("-") || d.equalsIgnoreCase("NaN")
                || d.equalsIgnoreCase("nil") || d.equalsIgnoreCase("_")) {
            return true;
        }

        return false;
    }

    /**
     * Format double to string.
     *
     * @param value source string
     * @param digit decimal space.
     * @return result
     */
    public static String formatDouble(Double value, int digit) {
        return String.format("%." + digit + "f", value);
    }

    /**
     * Format BigDecimal to string.
     *
     * @param value source string
     * @return result
     */
    public static String formatBigDecimal(String value) {
        BigDecimal bd = null;
        if (StringUtils.isNotBlank(value)) {
            try {
                bd = new BigDecimal(value);
                value = bd.toPlainString();
            } catch (Exception e) {
                Pattern pattern = Pattern.compile("^([0-9]{1,3}(,[0-9]{3})*(\\.[0-9]+)?|\\.[0-9]+)$");
                if (StringUtils.isNotBlank(value) && pattern.matcher(value).matches()) {
                    DecimalFormat df = new DecimalFormat();
                    try {
                        value = df.parse(value).toString();
                        bd = new BigDecimal(value);
                        value = bd.toPlainString();
                    } catch (ParseException e1) {
                    }
                }
            }
        }
        return value;
    }
}
